package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.pojo.Member;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("memberCon")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = memberService.findPage(queryPageBean);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,page);
    }

    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member){
        try {
            memberService.addMember(member);
            System.out.println(member+"left join");
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
    }

    @RequestMapping("/update")
    public Result updMember(@RequestBody Member member){
        try {
            memberService.updMember(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            memberService.queryById(id);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
    }

    @RequestMapping("/delete")
    public Result delMember(Integer id){
        try {
            memberService.delMember(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
    }
}
